//
//  YZGRefreshCollectionController.m
//  sisitv_ios
//
//  Created by apple on 17/3/1.
//  Copyright © 2017年 JLXX--YZG. All rights reserved.
//

#import "YZGRefreshCollectionController.h"
#import "YZGRequest.h"
@interface YZGRefreshCollectionController ()

@end

@implementation YZGRefreshCollectionController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createDataManager];
    self.collectionView.isHadRefreshAction = YES;
}

-(void)createDataManager{
    NSAssert(NO, @"You Must implement This Mehtod ---%@---- %@",NSStringFromClass([self class]),NSStringFromSelector(_cmd));
}

-(void)createRequest{
    NSAssert(NO, @"You Must implement This Mehtod ---%@---- %@",NSStringFromClass([self class]),NSStringFromSelector(_cmd));
}

-(void)requestDidSuccess{
    //子类做实际处理
    NSAssert(NO, @"You Must implement This Mehtod ---%@---- %@",NSStringFromClass([self class]),NSStringFromSelector(_cmd));
}

-(void)requestDidFaile{
    NSString *errorStr = self.listDataManager.netRequest.error.localizedDescription;
    if ([errorStr isEqualToString:@"用户信息异常"]) {
        UIAlertController *errorAlert = [UIAlertController alertControllerWithTitle:@"提示" message:@"登录信息过期，请重新登录" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [errorAlert dismissViewControllerAnimated:YES completion:nil];
        }];
        
        UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [errorAlert dismissViewControllerAnimated:YES completion:nil];
        }];
        
        [errorAlert addAction:cancel];
        [errorAlert addAction:ok];
        [self presentViewController:errorAlert animated:YES completion:nil];
    }
    //子类可以不处理
    //    if (self.listModel.isNetworkError) {
    //        NSString *errorString = [self.listModel.netRequest.error localizedDescription];
    //        [AlertTool ShowErrorInView:KKeyWindow withTitle:errorString];
    //    }else{
    //        [AlertTool Hidden];
    //    }
}

#pragma mark YZGCollectionViewDelegate方法
-(void)pullDownToRefresh{
    [self createRequest];
    [self.collectionView yzgResetFooter];
    [self.listDataManager pullDownToRefresh];
}

-(void)pullUpToLoadMore{
    [self createRequest];
    [self.listDataManager pullUpToLoadMore];
}

#pragma mark YZGListDataManagerProtocol方法

-(void)refreshNewSuccess:(YZGListDataManager *)listDataManager{
    if (listDataManager.isNeedLoadMore) {
        self.collectionView.isHadLoadMoreAction = YES;
    }
    [self.dataSource clearAllSectionItems];
    [self requestDidSuccess];
}

-(void)loadMoreSuccess:(YZGListDataManager *)listDataManager{
    [self requestDidSuccess];
}
-(void)requestFinishedSuccess:(YZGListDataManager *)listDataManager{
    [self removeNodataView];
    [self.collectionView yzgStopRefreshing];
    [self.collectionView reloadData];
}

-(void)didLoadLastPage:(YZGListDataManager *)listDataManager{
    [self.collectionView endLoadWithNoMoreData];
}

-(void)requestFinishedFaile:(YZGListDataManager *)listDataManager{
    [self.collectionView yzgStopRefreshing];
    if (self.listDataManager.isRefresh) {
        [self.dataSource clearAllSectionItems];
        [self.collectionView reloadData];
        [self addNodataViewInView:self.collectionView];
    }else{
        [self.collectionView endLoadWithNoMoreData];
    }
    [self requestDidFaile];
}


@end
